class SearchController extends BaseController{

    def beforeInterceptor = [action:this.auth, except:['login', 'dologin']]

    def show = {
        def a = session.access;
        a.refresh()
        if(a.hasDevAuth()){
            println("Developers cannot search.");
        }
        else{
            if(a && a.emp){
                Employee e = a.emp;
                Area area = a.area;

                //area.projects // List of projects.
                //area.courses // list of courses.
                if(params && params.keyword){
                    List<Project> lp = new ArrayList<Project>();
                    List<Course> lc = new ArrayList<Course>();
                    List<Project> validPrjList = new ArrayList<Project>();
                    List<Project> validCrsList = new ArrayList<Project>();
                    def cr = Project.createCriteria()
                    def resultsProjects = cr {
                            like("name", params.keyword+"%")
                            order("name", "desc")
                    }

                    def cp = Course.createCriteria()
                    def resultsCourses = cp {
                            like("name", params.keyword+"%")
                            order("name", "desc")
                    }

                    if(a.hasAdminAuth()){
                        for(Area ar in a?.container?.areas){
                            for(Project p in ar.projects){
                                validPrjList.add(p);
                            }
                            for(Course c in ar.courses){
                                validCrsList.add(c);
                            }
                        }
                    }
                    else{
                        for(Course c in area.courses){
                            validCrsList.add(c);
                        }
                        for(Project p in area.projects){
                            validPrjList.add(p);
                        }
                        validPrjList.add(area.projects);
                        validCrsList.add(area.courses);
                    }
                    
                    for(Project pr in resultsProjects){
                        if(validPrjList.contains(pr)){
                            lp.add(pr);
                        }
                    }

                    for(Course co in resultsCourses){
                        if(validCrsList.contains(co)){
                            lc.add(co);
                        }
                    }

                    if( (lp && lp.size() >0) || (lc && lc.size()>0) ){
                        return [courseList : lc, projectList: lp];
                    }
                    else{
                        flash.message = "No results found.";
                        return;
                    }
                }
                else{
                    return;
                }
            }
            else{
                redirect(uri:"/");
            }
        }
        redirect(uri:"/");
    }
}
